package edu.xidian.bdilab.jrbigdata.controller.government;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonResponse;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.service.government.register.GovernmentRegisterService;
import edu.xidian.bdilab.jrbigdata.util.AliyunConfigUtil;
import edu.xidian.bdilab.jrbigdata.util.GuavaCacheUtil;
import edu.xidian.bdilab.jrbigdata.util.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName GoveRegisterController
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/3/29 下午7:45
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@RestController
@Api(tags = {"governmentRegister-政府用户注册功能"})
public class GoveRegisterController {
    @Autowired
    GovernmentRegisterService governmentRegisterService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "govrenmentCode", value = "政府编码"),
            @ApiImplicitParam(name = "fkUserName", value = "用户名"),
            @ApiImplicitParam(name = "departmentName", value = "政府部门名称"),
            @ApiImplicitParam(name = "principalName", value = "负责人姓名"),
            @ApiImplicitParam(name = "principalPhone", value = "负责人手机号"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    @RequestMapping(value = "government/register", method = RequestMethod.POST)
    public ResponseResult register(@RequestParam int govrenmentCode,
                                   @RequestParam String fkUserName,
                                   @RequestParam String departmentName,
                                   @RequestParam String principalName,
                                   @RequestParam String principalPhone,
                                   @RequestParam String password) {

        ResponseResult responseResult = new ResponseResult();

        if (governmentRegisterService.checkGovernmentCode(govrenmentCode) == false) {
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.REGISTER_FAIL_GOVERNMENTCODE.getCode());
            responseResult.setCode(GovernmentCode.REGISTER_FAIL_GOVERNMENTCODE.getMessage());
            return responseResult;
        } else if (governmentRegisterService.checkUserName(fkUserName) == false) {
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.REGISTER_FAIL_USERNAME.getCode());
            responseResult.setMessage(GovernmentCode.REGISTER_FAIL_USERNAME.getMessage());
            return responseResult;
        } else {
            governmentRegisterService.setGovernmentInfo(govrenmentCode, fkUserName, departmentName, principalName, principalPhone);
            governmentRegisterService.setGovernmentUser(fkUserName, password);

            responseResult.setSuccess(true);
            responseResult.setCode(GovernmentCode.REGISTER_SUCCEESS.getCode());
            responseResult.setMessage(GovernmentCode.REGISTER_SUCCEESS.getMessage());
        }

        return responseResult;

    }

    @RequestMapping(value = "government/sendCode", method = RequestMethod.POST)
    public ResponseResult sendCode(@RequestParam String phoneNo) {
        ResponseResult responseResult = new ResponseResult();
        if (StringUtils.isEmpty(phoneNo)) {
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.SEND_CODE_FAIL.getCode());
            responseResult.setMessage(GovernmentCode.SEND_CODE_FAIL.getMessage());
            return responseResult;
        }
        String code = (String)GuavaCacheUtil.get(phoneNo);
        if(!StringUtils.isEmpty(code)){
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.SEND_CODE_REPEAT.getCode());
            responseResult.setMessage(GovernmentCode.SEND_CODE_REPEAT.getMessage());
            return responseResult;
        }
        /**
         * 发送验证码
         */
        String randomCode = MessageUtil.randomCode();
        System.out.println("发送验证码="+randomCode);
        Map<String,String> param = new HashMap<>();
        param.put("code",randomCode);
        CommonResponse commonResponse = MessageUtil.aliyunsendSms(AliyunConfigUtil.APP_ID,AliyunConfigUtil.APP_SECRET,phoneNo,
                AliyunConfigUtil.SIGNNAME,AliyunConfigUtil.TEMPLATE_CODE_REGISTER_CODE, param);
        JSONObject json = JSONObject.parseObject(commonResponse.getData());
        String resultCode =(String)json.get("Code");
        String message =(String)json.get("Message");
        if("OK".equals(resultCode) && "OK".equals(message)){
            GuavaCacheUtil.put(phoneNo,randomCode);
            responseResult.setSuccess(true);
            responseResult.setCode(GovernmentCode.SEND_CODE_SUCCESS.getCode());
            responseResult.setMessage(GovernmentCode.SEND_CODE_SUCCESS.getMessage());
            return responseResult;
        }
        GuavaCacheUtil.put(phoneNo,randomCode);
        responseResult.setSuccess(false);
        responseResult.setCode(GovernmentCode.SEND_CODE_FALI.getCode());
        responseResult.setMessage(GovernmentCode.SEND_CODE_FALI.getMessage());
        return responseResult;

    }

    @RequestMapping(value = "government/checkSendCode", method = RequestMethod.POST)
    public ResponseResult sendCode(@RequestParam String phoneNo,@RequestParam String messageCode) {
        ResponseResult responseResult = new ResponseResult();
        if (StringUtils.isEmpty(phoneNo) || StringUtils.isEmpty(messageCode)) {
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.SEND_CODE_FAIL.getCode());
            responseResult.setMessage(GovernmentCode.SEND_CODE_FAIL.getMessage());
            return responseResult;
        }


        Object code =GuavaCacheUtil.get(phoneNo);
        if(!messageCode.equals(code)){
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.SEND_CODE_FAIL.getCode());
            responseResult.setMessage(GovernmentCode.SEND_CODE_FAIL.getMessage());
            return responseResult;
        }
        responseResult.setSuccess(true);
        responseResult.setCode(GovernmentCode.SEND_CODE_NOT_EQ.getCode());
        responseResult.setMessage(GovernmentCode.SEND_CODE_NOT_EQ.getMessage());
        return responseResult;


    }

    public static void main(String[] args) {
        String s = MessageUtil.randomCode();
        System.out.println(s);
    }

}
